// component/msg/msg.js

// pages/maintenance/maintenance.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../utils/request.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    title: '',
    currenttab: '',
    showModal: false,
    items: [],
    imgUrl: applet_park_api_url_imgUrl,
    tab: [{name: '全部'},{name: '公告'},{name: '宅家有乐'},{name:'政企交流'}],
    currentIndex: 0
  },
 
  /* 组件声明周期函数 */
  lifetimes: {
    attached: function () {
      let id = wx.getStorageSync("id");
      this.setData({
        systemid: id
      })
      
      this.loadData();
    },
    moved: function () {
    },
    detached: function () {

    },
  },
  pageLifetimes: {
    show: function () {
      // 页面被展示

      let id = wx.getStorageSync("id");
      this.setData({
        systemid: id
      })

      this.loadData();
      // this.triggerEvent('swichNav', "22121")
      // debugger
      
    },
    hide: function () {
      // 页面被隐藏
      this.loadData();
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
     // tab栏切换
  tabChange(e){
    this.setData({
      currentIndex: e.currentTarget.dataset.index
    })
  },
    packBp(e) {
      this.setData({
        currenttab: e.currentTarget.dataset.index
      })
    },
    testSubmit(e) {
      var self = this;
      var openId = wx.getStorageSync("OPENID");
      var fId = e.detail.formId; //获取formId
      wx.request({
        url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxcc80ff4f9e7ee858&secret=88cdd694e5804167c07f5b89b5a56d20',
        method: 'GET',
        success: function (res) {

          var d = {
            "keyword1": {
              "value": "00273",
              "color": "#4a4a4a"
            },
            "keyword2": {
              "value": "腾讯早餐店",
              "color": "#9b9b9b"
            },
            "keyword3": {
              "value": "66元",
              "color": "#9b9b9b"
            }

          };
          wx.request({
            url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=' + res.data.access_token,
            // header: {
            //   'content-type': 'Content-Type: application/json;',
            // },
            data: {
              touser: openId,
              template_id: "BEaKeDwRS2aPA0xfsrbSlxh7NfFeM7rSDk5wd70_WNs", //换成你申请的模板消息id，  
              page: "pages/index/index",
              form_id: e.detail.formId,
              data: d

            },
            method: 'POST',
            success: function (res) {
            },
            fail: function (err) {
              
            }
          })
        },
      })
    },

    formSubmit: function (e) {
      var that = this
      wx.request({
        url: "https://xxx/mobiletplus/index.php?act=login&op=send_wx",
        data: {
          "form_id": e.detail.formId,
          "customer_id": that.data.customer_id
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          //util.showSuccess("设置成功")

        },
        fail: function (e) {
          //util.showError("设置失败".e)
        }
      })
    },

    openzp: function () {
      this.setData({
        showModal: true
      })
    },

    preventTouchMove: function () {

    },


    go: function () {
      this.setData({
        showModal: false
      })
    },
    loadData() {
      this.setData({
        title: '',
      })
   
      let that = this;
      let id = wx.getStorageSync("id");
      if (!id) {
        return
      }
      // wx.request({
      //   url: applet_park_api_url + '/api/notifications/index',
      //   data: {
      //     consignorId: id
      //   },
      //   success(res) {
      //     if (res.data.code == 0) {
        
      //       that.data.items = res.data.data;
      //       that.setData(that.data);
      //       if (res.data.data.length == 0) {
      //         that.setData({
      //           title: '暂无更多数据',
      //         })
      //       }else{
      //         that.setData({
      //           title: '',
      //         })
      //       }
      //     } else {
      //       wx.showToast({
      //         title: '网络异常',
      //       })
      //       that.setData({
      //         title: '暂无更多数据',
      //       })
      //     }
      //   },
      //   fail(res) {
      //     wx.showToast({
      //       title: res.data,
      //     })
      //   }
      // })

      requestDataparams('/api/notifications/index', { consignorId: id}, 'GET').then(res => {
        if (res.data.code == 0) {

          that.data.items = res.data.data;
          that.setData(that.data);
          if (res.data.data.length == 0) {
            that.setData({
              title: '暂无更多数据',
            })
          } else {
            that.setData({
              title: '',
            })
          }
        } else {
          wx.showToast({
            title: '网络异常',
          })
          that.setData({
            title: '暂无更多数据',
          })
        }
      }).catch(err => {
        
      });
    },
    goToDetail() {
      // pages / maintenanceDetail / maintenanceDetail
      wx.navigateTo({
        url: '../maintenanceDetail/maintenanceDetail',
      })
    },
    goMsgdetail(e) {
      let type = e.currentTarget.dataset.type;
      let assignorId = e.currentTarget.dataset.assignorid;
      let consignorId = e.currentTarget.dataset.consignorid;
      let todoStatusType = e.currentTarget.dataset.todostatustype;

      wx.navigateTo({
        url: '../msgDetail/msgDetail?type=' + type + '&assignorId=' + assignorId + '&consignorId=' + consignorId + '&todoStatusType=' + todoStatusType,
      })

    },
  },
  onShow: function () {
    this.loadData();
 
  },

})
